package com.zeny.coding.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.dto.MemberDto;
import com.zeny.coding.dto.PageDto;
import com.zeny.coding.entity.Member;
import com.zeny.coding.entity.MemberExample;
import com.zeny.coding.mapper.MemberMapper;
import com.zeny.coding.service.MemberService;
import com.zeny.coding.service.SmsService;
import com.zeny.coding.util.SnowFlake;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <h2>角色服务</h2>
 *
 * @Author Zeny
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private SmsService smsService;

    @Override
    public void list(PageDto<MemberDto> pageDto) {
        //分页
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        //设置条件
        MemberExample memberExample = new MemberExample();
        memberExample.setOrderByClause("id desc");
        //查询结果
        List<Member> memberList = this.memberMapper.selectByExample(memberExample);
        PageInfo<Member> pageInfo = new PageInfo<>(memberList);
        //设置总条数
        pageDto.setTotal(pageInfo.getTotal());
        //封装成DTO传输
        List<MemberDto> memberDtoList = new ArrayList<>();
        for (Member member : memberList) {
            MemberDto memberDto = new MemberDto();
            BeanUtils.copyProperties(member, memberDto);
            memberDtoList.add(memberDto);
        }
        //设置记录数
        pageDto.setList(memberDtoList);
    }

    @SuppressWarnings("all")
    @Transactional
    @Override
    public void save(MemberDto memberDto) {
        //DTO转~
        Member member = new Member();
        BeanUtils.copyProperties(memberDto, member);
        //算法算法生成ID
        SnowFlake snowFlake = new SnowFlake(
                CourseConstants.dataCenterId,
                CourseConstants.machineId);
        member.setId(String.valueOf(snowFlake.nextId()));
        member.setRegisterTime(new Date());
        this.memberMapper.insertSelective(member);
        //更新验证码状态
        smsService.updateStatus(memberDto.getMobile(), memberDto.getSmsCode());
    }

    @Override
    public MemberDto login(MemberDto memberDto) {
        //根据手机号码查询用户信息
        MemberExample example = new MemberExample();
        example.createCriteria().andMobileEqualTo(memberDto.getMobile());
        List<Member> memberList = this.memberMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(memberList)) {
            throw new RuntimeException(CourseConstants.MEMBER_NOT_EXIST);
        }
        //检查密码
        Member member = memberList.get(0);
        if (!memberDto.getPassword().equalsIgnoreCase(member.getPassword())) {
            //密码错误
            throw new RuntimeException(CourseConstants.MEMBER_PASSWORD_ERROR);
        }
        BeanUtils.copyProperties(member, memberDto);
        //清空密码
        memberDto.setPassword(null);
        return memberDto;
    }

    @Override
    public boolean isMobileExist(String mobile) {
        MemberExample example = new MemberExample();
        example.createCriteria().andMobileEqualTo(mobile);
        List<Member> memberList = memberMapper.selectByExample(example);
        return !CollectionUtils.isEmpty(memberList);
    }

    @SuppressWarnings("all")
    @Transactional
    @Override
    public void updateMember(MemberDto memberDto) {
        MemberExample example = new MemberExample();
        MemberExample.Criteria criteria = example.createCriteria();
        //手机号码
        criteria.andMobileEqualTo(memberDto.getMobile());
        Member member = new Member();
        member.setPassword(memberDto.getPassword());
        memberMapper.updateByExampleSelective(member, example);
        //更新验证码状态
        smsService.updateStatus(memberDto.getMobile(), memberDto.getSmsCode());
    }
}
